package com.hnhp.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class ChatController {
    //引入ollama的接口
    @Autowired
//    @Resource
    private OllamaChatModel ollamaChatModel;


    //普通输出方式
    @GetMapping("/chat/{msg}")
    public String chat(@PathVariable() String msg){
        return ollamaChatModel.call(msg);
    }

    //流式输出
    @GetMapping(value = "/stream",produces = "text/event-stream;charset=utf-8")
    public Flux<String> chatStream(@RequestParam(value = "message") String message){
        //创建流式 信息输入对象
        Prompt prompt= new Prompt(new UserMessage(message));
       return ollamaChatModel.stream(prompt.getContents());

    }
}
